
#The PLSQLGen class contains the necessary components to 
#process the PLSQLGen configuration file and to
#generate PLSQL files from erb templates

class PLSQLGen

  #using the incoming string properties_filename (which should include a path)
  #to load the contents of the file into a hash object (keys and value pairs)
  #and return it to the calling method
  
  def load_properties(properties_filename)
    properties = {}
    File.open(properties_filename, 'r') do |properties_file|
      properties_file.read.each_line do |line|
        line.strip!
        if (line[0] != ?# and line[0] != ?=)
          i = line.index(':')
          if (i)
            properties[line[0..i - 1].strip] = line[i + 1..-1].strip
          else
            properties[line] = ''
          end
        end
      end      
    end
    return properties
  end
  
  #simply moves the incoming file object into a string object
  #then returns the string object to the calling method
  
  def file_to_string( a_file )
    the_string = ""
    a_file.each do |line|
      the_string += line
    end
    return the_string
  end
  
  #parameter the_string is an erb template
  #parameter the_file_name is the file path and name of the file 
  #to which the processed template will be saved
  
  def erb_to_file( the_string, the_file_name )
    the_template = nil
    new_file = nil
    #make an erb template out of the string and translate result
    the_template = ERB.new( the_string, nil, ">" ).result(binding)
    #save the template to file
    new_file = File.new( the_file_name, "w+" )
    #print data to the file
    new_file.print( the_template )
  end
  
end